// pages/teacher/teacherWorkSure/teacherWorkSure.js
import {teacher_active} from "../../../../asserts/js/common/barList";
import {post} from "../../../../asserts/js/request";
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    barList: teacher_active(0),
    top: app.globalData.bottom + 'px',
    stuList: [],
    classId: '',
    allChoose: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      classId: options.classId
    })
    var list = wx.getStorageSync('subList');
    var flag = true;
    toFor:
    for(var item of list) {
      for(var el of item.studentList) {
        if(!el.active){
          flag = false;
          break toFor
        }
      }
    }
    this.setData({
      stuList:list,
      allChoose: flag
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  back: function() {
    wx.navigateBack({
      delta: 0,
    })
  },
  stuChoose: function(e) {
    this.setData({
      ["stuList[" + e.detail.itemindex + "].studentList["+ e.detail.elindex +"].active"]: e.detail.value
    })
    if(e.detail.value) {
      var flag = true;
      toFor:
      for(var item of this.data.stuList){
        for(var el of item.studentList) {
          if(!el.active){
            flag = false;
            break toFor;
          }
        }
      }
      this.setData({
        allChoose: flag
      })
    }else {
      this.setData({
        allChoose: false
      })
    }
  },
  sure: function() {
    var this_ = this;
    var studentList = [];
    var courseList = [];
    for(var item of this.data.stuList) {
      for(var el of item.studentList) {
        if(el.active && el.courseList.length > 0){
          studentList.push(el.studentId);
          courseList = el.courseList
        }
      }
    }
    if(studentList.length == 0) {
      wx.showToast({
        title: '请选择学生',
        icon: "none"
      })
      return
    }
    post("addHomework", {
      courseId: courseList.map(el => {
        return el.id
      }).join("|"),
      studentId: studentList.join("|"),
      teacherId: app.globalData.teacherInfo.teacherId,
      classId: this_.data.classId
    }).then(data => {
      console.log(data)
      wx.showToast({
        title: '布置成功',
        icon: 'none'
      })
      wx.redirectTo({
        url: '/pages/teacher/myclass/teacherClass/teacherClass',
      })
    })
  },
  allChoose: function() {
    let this_ = this
    this.setData({
      stuList: this.data.stuList.map(item => {
        item.studentList = item.studentList.map(el => {
          el.active = !this_.data.allChoose;
          return el;
        })
        return item;
      }),
      allChoose: !this_.data.allChoose
    })
  }
})